﻿using System;
using System.Collections.Generic;
using System.Text;

namespace lxwmvc
{
    using System;
    using System.Xml;
    using System.Web;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Data;

    public class funhelper
    {
        /// <summary>
        /// 加法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object jiafa(object a, object b)
        {
            return (a.todecimal() + b.todecimal());
        }


        /// <summary>
        /// 减法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object jianfa(object a, object b)
        {
            return (a.todecimal() - b.todecimal());
        }

        /// <summary>
        /// 乘法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object chengfa(object a, object b)
        {
            return (a.todecimal() * b.todecimal());
        }

        public static bool panduan(object a, object c, object b)
        {
            string biaodanshi = c.tostr();
            if (biaodanshi == ">=")
                return a.todecimal(0) >= b.todecimal(0);
            else if (biaodanshi == ">")
                return a.todecimal(0) > b.todecimal(0);
            else if (biaodanshi == "=")
                return a.todecimal(0) == b.todecimal(0);
            else if (biaodanshi == "<")
                return a.todecimal(0) < b.todecimal(0);
            else if (biaodanshi == "<=")
                return a.todecimal(0) <= b.todecimal(0);
            else if (biaodanshi == "!=")
                return a.todecimal(0) != b.todecimal(0);

            return false;
        }

        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object chufa(object a, object b)
        {
            return (a.todecimal() / b.todecimal());
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static object todecimal(object a)
        {
            return a.todecimal(0);
        }

        public static object toint(object a)
        {
            return a.toint32(0);
        }

        /// <summary>
        /// 余数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object yushu(object a, object b)
        {
            return (a.toint32() % b.toint32());
        }

        /// <summary>
        /// 去掉后面的0和小数点
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static object qu0(object strNu)
        {
            string[] numArr = strNu.tostr().Split('.');

            //去除小数点钱多余的0，比如000.11处理为0.11 根据需要保留      
            for (int k = 0; k < numArr[0].Length; k++)
            {
                if (numArr[0].ToCharArray()[k] != '0')
                {
                    numArr[0] = numArr[0].Substring(k, numArr[0].Length); break;
                }
                else
                {
                    numArr[0] = "0";
                }
            }

            if (numArr.Length < 2)
            //判断是否有小数点  
            {
                return numArr[0];
            }
            string strNum = numArr[1];
            char[] cArr = strNum.ToCharArray();
            int index = -1;

            //获取最后一个出现非0的位置  
            for (int i = 0; i < strNum.Length; i++)
            {
                if (cArr[i] != '0') { index = i; }
            }

            //判断小数点后是否全是0 
            if (index == -1)
            {
                return numArr[0];
            }
            else
            {
                return numArr[0] + "." + strNum.Substring(0, index + 1);
            }
        }

        /// <summary>
        /// 输出json对象
        /// </summary>
        /// <param name="count"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string json(int count, DataTable dt)
        {
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            string result = "";
            var json = new { total = count, rows = dt };
            result = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented, timeConverter);
            return result;
        }

        /// <summary>
        /// 分割字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="splitstr"></param>
        /// <returns></returns>
        public static string[] split(object obj, string splitstr = ",")
        {
            return stringhelper.split(obj.tostr(), splitstr);
        }

        /// <summary>
        /// #foreach($item in $funhelper.array("a","b","c","d"))
        /// $item.key $item.value
        /// #end
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static List<object> array(params object[] args)
        {
            if (args == null || args.Length == 0) return null;
            List<object> lst = new List<object>();
            for (int i = 0; i < args.Length / 2; i++)
            {
                lst.Add(new { key = args[i * 2], value = args[i * 2 + 1] });
            }

            return lst;
        }

        /// <summary>
        /// 强大的switch函数 
        /// </summary>
        /// <param name="v"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static object encode(object v, params object[] args)
        {
            if (args == null) return "";
            bool b = args.Length % 2 == 0;

            //if (b)
            {
                for (int i = 0; i < args.Length / 2; i++)
                {
                    if (args[i * 2].tostr() == v.tostr()) return args[i * 2 + 1];
                }
            }


            if (!b) return args[args.Length - 1];

            return "";
        }
    }
}